package com.cn.unarmed.activity;

import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.animation.LinearInterpolator;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.cn.unarmed.base.BaseActivity;
import com.cn.unarmed.commonutils.AppUtils;
import com.cn.unarmed.commonutils.MyAnimatorUpdateListener;
import com.cn.unarmed.views.DialogOfMyFirst;
import com.cn.unarmed.views.RoundView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class MainActivity extends BaseActivity {

    TextView textView, textView_name;
    ImageView imageView;
    List<HashMap<String, Object>> list;
    boolean isFirst = true;
    MyAnimatorUpdateListener listener;
    RoundView roundView;
    Button btn_ex_listView;

    @Override
    protected void HandleTitleBarEvent(TitleBar component, View v) {
        if (component.equals(TitleBar.TITLE)){
            AppUtils.showToast(this,"你丫点到我了",false);
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        setTitle("prpprpr");
        initView();
    }

    @Override
    protected void onResume() {
        super.onResume();
        initData();
    }

    public void initView() {

        textView = (TextView) this.findViewById(R.id.text_name);
        textView_name = (TextView) this.findViewById(R.id.name_value);
        imageView = (ImageView)this.findViewById(R.id.image);
        roundView = (RoundView)this.findViewById(R.id.round);
        btn_ex_listView = (Button)this.findViewById(R.id.btn_ex_listView);
        btn_ex_listView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MainActivity.this,ChildListViewActivity.class));
            }
        });

        LinearInterpolator lin = new LinearInterpolator();
        final ObjectAnimator anim = ObjectAnimator.ofFloat(imageView, "rotation", 0f, 360f);//设置动画为旋转动画，角度是0-360
        anim.setDuration(5000);//时间15秒，这个可以自己酌情修改
        anim.setInterpolator(lin);
        anim.setRepeatMode(ValueAnimator.RESTART);//设置重复模式为重新开始
        anim.setRepeatCount(-1);//重复次数为-1，就是无限循环
        listener = new MyAnimatorUpdateListener(anim);//将定义好的ObjectAnimator传给MyAnimatorUpdateListener监听
        anim.addUpdateListener(listener);//给动画加监听
//        setPlay(anim);

        imageView.setClickable(true);
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setPlay(anim);
            }
        });
        Button button = (Button)this.findViewById(R.id.check_list);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MainActivity.this,CheckableFrameLayoutActivity.class));
            }
        });
        Button button_2 = (Button)this.findViewById(R.id.check_list_2);
        button_2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MainActivity.this,CheckableLinearLayoutActivity.class));
            }
        });

        Button button_3 = (Button)this.findViewById(R.id.bitmap_clip);
        button_3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MainActivity.this,QQHeaderActivity.class));
            }
        });
        Button just_turn = (Button)this.findViewById(R.id.just_turn);
        just_turn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MainActivity.this,SetViewStyleActivity.class));
            }
        });

        Button btn_swipe = (Button)this.findViewById(R.id.just_turn_swipe);
        btn_swipe.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MainActivity.this,SwipeItemActivity.class));
            }
        });
        Button view_06_2017 = (Button)this.findViewById(R.id.view_06_2017);
        view_06_2017.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(MainActivity.this,TwoListViewActivity.class));
            }
        });
        Button view_jump = (Button)this.findViewById(R.id.view_jump);
        view_jump.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MainActivity.this,ViewJumpActivity.class));
            }
        });
    }

    public void setPlay(ObjectAnimator anim){
        if (isFirst){
            anim.start();
            isFirst = false;
        } else {
            if (listener.isPause()){
                listener.play();
            }else if (listener.isPlay()){
                listener.pause();
            }
        }
    }

    public void initData() {

        list = new ArrayList<>();
        HashMap<String, Object> map_0 = new HashMap<>();
        map_0.put("text", "prprprpr");
        HashMap<String, Object> map_1 = new HashMap<>();
        map_1.put("text", "blablabla");
        HashMap<String, Object> map_2 = new HashMap<>();
        map_2.put("text", "blahblah");
        list.add(map_0);
        list.add(map_1);
        list.add(map_2);

        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new DialogOfMyFirst(MainActivity.this)
                        .builder(list)
                        .setListClick(new DialogOfMyFirst.OnItemClickListener() {
                            @Override
                            public void getItem(HashMap<String, Object> map) {
                                if (map.get("text").toString() != null) {
                                    textView_name.setText(map.get("text").toString());
                                }
                            }
                        })
                        .setCancel(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                            }
                        })
                        .setTitle("prprprp")
                        .setJump(new DialogOfMyFirst.OnButtonClickListener() {
                            @Override
                            public void getButton(HashMap<String, Object> map) {
                            }
                        })
                        .show();
            }
        });
    }
}
